home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Movie / Sources / MoviePar.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  14.6 KB  |  497 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                MoviePar.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                Lonnie Millett
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef _MOVIEPAR_
  14. #include "MoviePar.h"
  15. #endif
  16.  
  17. #ifndef _MOVIEFRA_
  18. #include "MovieFra.h"
  19. #endif
  20.  
  21. #ifndef _MOVIEFAC_
  22. #include "MovieFac.h"
  23. #endif
  24.  
  25. #ifndef _MOVIESEL_
  26. #include "MovieSel.h"
  27. #endif
  28.  
  29. // ----- Framework Includes -----
  30.  
  31. #ifndef FWMEMMGR_H
  32. #include <FWMemMgr.h>
  33. #endif
  34.  
  35. #ifndef FWSELECT_H
  36. #include "FWSelect.h"
  37. #endif
  38.  
  39. #ifndef FWUTIL_H
  40. #include "FWUtil.h"
  41. #endif
  42.  
  43. // ----- OpenDoc Includes -----
  44.  
  45. #ifndef _STORAGEU_
  46. #include <StorageU.h>
  47. #endif
  48.  
  49. #ifndef _FRAME_
  50. #include <Frame.h>
  51. #endif
  52.  
  53. #ifndef _FACET_
  54. #include <Facet.h>
  55. #endif
  56.  
  57. #ifndef _SHAPE_
  58. #include <Shape.h>
  59. #endif
  60.  
  61. #ifndef _WINDOW_
  62. #include <Window.h>
  63. #endif
  64.  
  65. #ifndef _MENUBAR_
  66. #include <MenuBar.h>
  67. #endif
  68.  
  69. #ifndef _XMPSESSM_
  70. #include <XMPSessM.h>
  71. #endif
  72.  
  73. #ifndef _TRANSLAT_
  74. #include <Translat.h>
  75. #endif
  76.  
  77. #ifndef _ARBITRAT_
  78. #include <Arbitrat.h>
  79. #endif
  80.  
  81. #ifndef _DISPTCH_
  82. #include <Disptch.h>
  83. #endif
  84.  
  85. #ifndef _EXCEPT_
  86. #include <Except.h>
  87. #endif
  88.  
  89. #ifndef _WINSTAT_
  90. #include <WinStat.h>
  91. #endif
  92.  
  93. #ifndef _STDTYPES_
  94. #include <StdTypes.h>
  95. #endif
  96.  
  97. #ifndef _STDPROPS_
  98. #include <StdProps.h>
  99. #endif
  100.  
  101. #ifndef _CMDDEFS_
  102. #include <CmdDefs.h>
  103. #endif
  104.  
  105. #if defined(FW_BUILD_MAC) && !defined(__GESTALTEQU__)
  106. #include <GestaltEqu.h>
  107. #endif
  108.  
  109. #if defined(FW_BUILD_MAC) && !defined(__ERRORS__)
  110. #include <Errors.h>
  111. #endif
  112.  
  113. #if defined(FW_BUILD_MAC) && !defined(__RESOURCES__)
  114. #include <Resources.h>
  115. #endif
  116.  
  117. #pragma segment MoviePart
  118.  
  119. //==============================================================================
  120. // Constants
  121. //==============================================================================
  122.  
  123. const short kChooseMovieItemID = 1;
  124. const short kStartMovieItemID = 3;
  125. const short kStopMovieItemID = 4;
  126. const short kSetMovieLoopingItemID = 6;
  127.  
  128. const short kDefaultMoviePicture = 2128;
  129.  
  130. const short kMovieIdleFreq = 1;
  131.  
  132. // Property for annotating our external movie information
  133. const XMPPropertyName    kPropQTMovieLooping  = "Apple:OpenDoc:Quality:kQTMovieLooping";
  134.  
  135. //----------------------------------------------------------------------------------------
  136. // CMoviePart::CMoviePart
  137. //----------------------------------------------------------------------------------------
  138. CMoviePart::CMoviePart()
  139. {
  140.     fQuickTimeInstalled = FALSE;
  141.     fMovieHandle = NULL;
  142.     fLooping = FALSE;
  143. }
  144.  
  145. //----------------------------------------------------------------------------------------
  146. // CMoviePart::~CMoviePart
  147. //----------------------------------------------------------------------------------------
  148. CMoviePart::~CMoviePart()
  149. {
  150.     if (fMovieHandle)
  151.     {
  152.         FW_CMemoryManager::FreeSystemHandle(fMovieHandle);
  153.         fMovieHandle = NULL;
  154.     }
  155.         
  156.     if (fQuickTimeInstalled)
  157.         ExitMovies();
  158. }
  159.  
  160. //----------------------------------------------------------------------------------------
  161. // CMoviePart::Release
  162. //----------------------------------------------------------------------------------------
  163. void CMoviePart::Release()
  164. {
  165.     FW_CPart::Release();
  166.     
  167.     if (this->GetRefCount() == 1)
  168.     {
  169.         this->GetSession()->GetDispatcher()->UnregisterIdle(this, NULL);
  170.     }
  171. }
  172.  
  173. //----------------------------------------------------------------------------------------
  174. // CMoviePart::Initialize
  175. //----------------------------------------------------------------------------------------
  176. void CMoviePart::Initialize()
  177. {
  178.     FW_CPart::Initialize();
  179.     
  180.     short error;
  181.     long    result;
  182.     
  183.     XMPSession* session = this->GetSession();
  184.     
  185.     error = Gestalt(gestaltQuickTime, &result);
  186.     fQuickTimeInstalled = (error == noErr);
  187.     
  188.     if (fQuickTimeInstalled)
  189.         THROW_IF_ERROR(EnterMovies());
  190.     else
  191.         THROW_IF_ERROR(envNotPresent, "MoviePart:Init:Quicktime not available");   
  192.  
  193.     FW_CAcquireASLMResourceAccess aq;
  194.     
  195.     XMPMenuBar *menuBar = this->GetMenuBar();
  196.     menuBar->AddMenuLast(kMovieMenu, GetMenu(kMovieMenu), this);
  197.  
  198.     menuBar->RegisterCommand(cChooseMovie, kMovieMenu, kChooseMovieItemID);
  199.     menuBar->RegisterCommand(cStartMovie, kMovieMenu, kStartMovieItemID);
  200.     menuBar->RegisterCommand(cStopMovie, kMovieMenu, kStopMovieItemID);
  201.     menuBar->RegisterCommand(cSetMovieLooping, kMovieMenu, kSetMovieLoopingItemID);
  202.     
  203.     this->GetSession()->GetDispatcher()->RegisterIdle(this, NULL, kMovieIdleFreq);
  204.     
  205.     FW_PlatformPict picture = ::GetPicture(kDefaultMoviePicture);
  206.     ::DetachResource((Handle)picture);
  207.     FW_CPictShape pictShape(picture);
  208.     fDefaultPicture = pictShape;
  209. }
  210.  
  211. //------------------------------------------------------------------------------
  212. // CMoviePart::InternalizeContent
  213. //------------------------------------------------------------------------------
  214. void CMoviePart::InternalizeContent(XMPStorageUnit* storageUnit)
  215. {
  216.     storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, this->GetContentPropertyValueType(), 0, kXMPPosUndefined);
  217.  
  218.     unsigned long movieSize = storageUnit->GetSize();
  219.     if (movieSize)
  220.     {
  221.         FW_PlatformHandle movieHandle = FW_CMemoryManager::AllocateSystemHandle(movieSize);
  222.         THROW_IF_NULL(movieHandle);
  223.         
  224.         FW_CMemoryManager::LockSystemHandle(movieHandle);
  225.         storageUnit->GetValue(movieSize, (XMPValue)*movieHandle);
  226.         FW_CMemoryManager::UnlockSystemHandle(movieHandle);
  227.         this->SetMovieHandle(movieHandle);
  228.     
  229.         storageUnit->Focus(kContentAnnotationProp, kXMPPosUndefined, kContentAnnotationValue, (XMPValueIndex)0, kXMPPosFirstSib);
  230.         storageUnit->GetValue(sizeof(FW_Boolean), (XMPValue)&fLooping);
  231.     }
  232. }
  233.  
  234. //----------------------------------------------------------------------------------------
  235. // CMoviePart::ExternalizeContent
  236. //----------------------------------------------------------------------------------------
  237. void CMoviePart::ExternalizeContent(XMPStorageUnit* storageUnit)
  238. {
  239.     CMovieFacet* facet = (CMovieFacet*)this->GetActiveFacet();
  240.     if (facet != NULL && facet->GetMovie())
  241.     {
  242.         FW_PlatformHandle movieHandle = FW_CMemoryManager::AllocateSystemHandle(0);
  243.         THROW_IF_NULL(movieHandle);
  244.         THROW_IF_ERROR(::PutMovieIntoHandle(facet->GetMovie(), movieHandle));
  245.         
  246.         FW_CMemoryManager::LockSystemHandle(movieHandle);
  247.         storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, this->GetContentPropertyValueType(), 0, kXMPPosUndefined);
  248.         storageUnit->DeleteValue(storageUnit->GetSize());
  249.         storageUnit->SetValue(FW_CMemoryManager::GetSystemHandleSize(movieHandle), (XMPValue)*movieHandle);
  250.         FW_CMemoryManager::UnlockSystemHandle(movieHandle);
  251.         FW_CMemoryManager::FreeSystemHandle(movieHandle);
  252.         
  253.         storageUnit->Focus(kContentAnnotationProp, kXMPPosUndefined, kContentAnnotationValue, (XMPValueIndex)0, kXMPPosFirstSib);
  254.         storageUnit->SetValue(sizeof(FW_Boolean), (XMPValue)&fLooping);
  255.     }
  256. }
  257.  
  258. //----------------------------------------------------------------------------------------
  259. // CMoviePart::AddProperties
  260. //----------------------------------------------------------------------------------------
  261. void CMoviePart::AddProperties(XMPStorageUnit* storageUnit)
  262. {
  263.     storageUnit->AddProperty(kContentAnnotationProp)->AddValue(kContentAnnotationValue);
  264. }
  265.  
  266. //----------------------------------------------------------------------------------------
  267. // CMoviePart::GetContentPropertyValueType
  268. //----------------------------------------------------------------------------------------
  269. XMPValueType CMoviePart::GetContentPropertyValueType() const
  270. {
  271.     // Our content type is the standard type for movies
  272.     return kSampleMovieKind;
  273. }
  274.  
  275. //----------------------------------------------------------------------------------------
  276. // CMoviePart::SetMovieHandle
  277. //----------------------------------------------------------------------------------------
  278. void CMoviePart::SetMovieHandle(FW_PlatformHandle movieHandle)
  279. {
  280.     if (fMovieHandle)
  281.         FW_CMemoryManager::FreeSystemHandle(fMovieHandle);
  282.         
  283.     fMovieHandle = movieHandle;
  284. }
  285.  
  286. //----------------------------------------------------------------------------------------
  287. // CMoviePart::SetMovieLooping
  288. //----------------------------------------------------------------------------------------
  289. void CMoviePart::SetMovieLooping(FW_Boolean loop)
  290. {
  291.     fLooping = loop;
  292. }
  293.  
  294. //----------------------------------------------------------------------------------------
  295. // CMoviePart::GetNewMovieFromMovieHandle
  296. //----------------------------------------------------------------------------------------
  297. Movie CMoviePart::GetNewMovieFromMovieHandle()
  298. {
  299.     Movie newMovie;
  300.     FW_Boolean dataRefChanged;
  301.     
  302.     THROW_IF_ERROR(::NewMovieFromHandle(&newMovie, fMovieHandle, newMovieActive, &dataRefChanged));
  303.     
  304.     return newMovie;
  305. }
  306.  
  307. //----------------------------------------------------------------------------------------
  308. // CMoviePart::NewFrame
  309. //----------------------------------------------------------------------------------------
  310. FW_CFrame* CMoviePart::NewFrame(XMPFrame* xmpFrame, XMPTypeToken presentation)
  311. {
  312.     FW_UNUSED(presentation);
  313.     
  314.     CMovieFrame *frame = new CMovieFrame();
  315.     frame->InitMovieFrame(xmpFrame, this);
  316.     return frame;
  317. }
  318.  
  319. //----------------------------------------------------------------------------------------
  320. // CMoviePart::NewSelection
  321. //----------------------------------------------------------------------------------------
  322. FW_CSelection* CMoviePart::NewSelection()
  323. {
  324.     CMovieSelection* movieSelection = new CMovieSelection;
  325.     movieSelection->InitMovieSelection(this);
  326.     return movieSelection;
  327. }
  328.  
  329. //------------------------------------------------------------------------------
  330. // CMoviePart::DoMenuEvent
  331. //------------------------------------------------------------------------------
  332.  
  333. FW_Boolean CMoviePart::DoMenuEvent(XMPMenuBar *menuBar, XMPCommandID commandID)
  334. {
  335.     FW_UNUSED(menuBar);
  336.     FW_Boolean menuHandled = FALSE;
  337.     
  338.     switch (commandID)
  339.     {
  340.         case cChooseMovie:
  341.             {
  342.                 AliasHandle movieAlias = this->GetMovieFileFromUser();
  343.                 if (movieAlias)
  344.                 {
  345.                     FW_CPartFrameIterator iter(this, this->GetDefaultPresentation());
  346.                     CMovieFrame* frame;
  347.                     while (!iter.IsDone())
  348.                     {
  349.                         frame = (CMovieFrame*)iter.CurrentItem();
  350.                         Movie newMovie = this->GetMovieFromAlias(movieAlias);
  351.                         frame->SetNewMovie(newMovie);
  352.                         iter.Next();
  353.                     }
  354.                     FW_CMemoryManager::FreeSystemHandle((FW_PlatformHandle)movieAlias);
  355.                 }
  356.             }
  357.             menuHandled = TRUE;
  358.             break;
  359.                 
  360.         case cSetMovieLooping:
  361.                 this->ToggleLooping();
  362.                 menuHandled = TRUE;
  363.                 break;
  364.                 
  365.         default:
  366.             menuHandled = FW_CPart::DoMenuEvent(menuBar, commandID);
  367.     }            
  368.     
  369.     return menuHandled;
  370. }
  371.  
  372. //----------------------------------------------------------------------------------------
  373. // CMoviePart::DoAdjustMenus
  374. //----------------------------------------------------------------------------------------
  375. void CMoviePart::DoAdjustMenus(XMPMenuBar* menuBar)
  376. {
  377.     FW_CPart::DoAdjustMenus(menuBar);
  378.     
  379.     if (HasPropertyOnClipboard(kXMPPropContents, this->GetContentPropertyValueType()))
  380.         menuBar->EnableCommand(kXMPCommandPaste, TRUE);
  381.         
  382.     if (((CMovieFacet*)this->GetActiveFacet())->GetMovie())
  383.     {
  384.         XMPTranslation *translate = GetSession()->GetTranslation();
  385.         XMPType PICTType = translate->GetISOTypeFromPlatformType('PICT', kXMPPlatformDataType);
  386.         XMPType TEXTType = translate->GetISOTypeFromPlatformType('TEXT', kXMPPlatformDataType);
  387.         XMPType sndType = translate->GetISOTypeFromPlatformType('snd ', kXMPPlatformDataType);
  388.         
  389.         if (HasPropertyOnClipboard(kXMPPropContents, PICTType) ||
  390.             HasPropertyOnClipboard(kXMPPropContents, TEXTType) ||
  391.             HasPropertyOnClipboard(kXMPPropContents, sndType))
  392.             menuBar->EnableCommand(kXMPCommandPaste, TRUE);
  393.         menuBar->EnableCommand(cSetMovieLooping, TRUE);
  394.         menuBar->EnableCommand(kXMPCommandSelectAll, TRUE);
  395.     }
  396.     else
  397.     {
  398.         menuBar->EnableCommand(kXMPCommandPaste, FALSE);
  399.         menuBar->EnableCommand(kXMPCommandSelectAll, FALSE);
  400.         menuBar->EnableCommand(cSetMovieLooping, FALSE);
  401.     }
  402.  
  403.     menuBar->EnableCommand(cChooseMovie, TRUE);
  404.     menuBar->CheckCommand(cSetMovieLooping, fLooping);
  405. }
  406.  
  407. //----------------------------------------------------------------------------------------
  408. // CMoviePart::DoIdle
  409. //----------------------------------------------------------------------------------------
  410. FW_Boolean CMoviePart::DoIdle()
  411. {
  412.     // Update all the display frames of the same type
  413.     FW_CPartFrameIterator iter(this, this->GetDefaultPresentation());
  414.     while (!iter.IsDone())
  415.     {
  416.         ((CMovieFrame*)iter.CurrentItem())->UpdateMovie();
  417.         iter.Next();
  418.     }
  419.     return TRUE;
  420. }
  421.  
  422. //----------------------------------------------------------------------------------------
  423. // CMoviePart::GetMovieFromUser
  424. //----------------------------------------------------------------------------------------
  425. AliasHandle CMoviePart::GetMovieFileFromUser()
  426. {
  427.     SFTypeList typeList;
  428.     StandardFileReply reply;
  429.     AliasHandle movieFileAlias = NULL;
  430.  
  431.     typeList[0] = MovieFileType;
  432.     
  433.     this->GetSession()->GetWindowState()->DeactivateFrontWindows();
  434.     ::StandardGetFilePreview ((FileFilterProcPtr)NULL, 1, typeList, &reply);
  435.     this->GetSession()->GetWindowState()->ActivateFrontWindows();
  436.  
  437.     if (reply.sfGood == true)
  438.         THROW_IF_ERROR(::NewAlias(NULL, &reply.sfFile, &movieFileAlias));
  439.     return  movieFileAlias;
  440. }
  441.  
  442. //----------------------------------------------------------------------------------------
  443. // CMoviePart::GetMovieFromAlias
  444. //----------------------------------------------------------------------------------------
  445. Movie CMoviePart::GetMovieFromAlias(AliasHandle movieAlias)
  446. {
  447.     Movie aMovie;
  448.     short movieResFile;
  449.     short movieResID;
  450.     Str255 movieName;
  451.     FW_Boolean wasChanged;
  452.     FSSpec movieFile;
  453.  
  454.     aMovie = NULL;
  455.     THROW_IF_ERROR(::ResolveAlias(NULL, movieAlias, &movieFile, &wasChanged));
  456.     THROW_IF_ERROR(::OpenMovieFile(&movieFile, &movieResFile, fsRdPerm));
  457.     movieResID = 0;    // First movie found
  458.     THROW_IF_ERROR(::NewMovieFromFile(&aMovie, movieResFile,
  459.                                     &movieResID,
  460.                                     movieName, newMovieActive,
  461.                                     &wasChanged));
  462.     THROW_IF_ERROR(::CloseMovieFile(movieResFile));
  463.     return  aMovie;
  464. }
  465.  
  466. //----------------------------------------------------------------------------------------
  467. // CMoviePart::ToggleLooping
  468. //----------------------------------------------------------------------------------------
  469. void CMoviePart::ToggleLooping()
  470. {
  471.     fLooping = !fLooping;
  472.     
  473.     FW_CPartFrameIterator iter(this, this->GetDefaultPresentation());
  474.     while (!iter.IsDone())
  475.     {
  476.         ((CMovieFrame*)iter.CurrentItem())->ToggleLooping(fLooping);
  477.         iter.Next();
  478.     }
  479. }
  480.  
  481. //----------------------------------------------------------------------------------------
  482. // CMoviePart::GetAnyExistingFacet
  483. //----------------------------------------------------------------------------------------
  484. FW_CFacet* CMoviePart::GetAnyExistingFacet()
  485. {
  486.     FW_CFacet* facet = NULL;
  487.     FW_CPartFrameIterator iter1(this, this->GetDefaultPresentation());
  488.     while (!iter1.IsDone())
  489.     {
  490.         FW_CFrameFacetIterator iter2(iter1.CurrentItem());
  491.         if (!iter2.IsDone())
  492.             return iter2.CurrentItem();
  493.         iter1.Next();
  494.     }
  495.     return facet;
  496. }
  497.